package com.ohuang.jiaxiaotong.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.bunny.android.library.LoadDataLayout;
import com.example.myhttp.CallBackObjects;
import com.example.myhttp.Ihttp;
import com.ohuang.jiaxiaotong.Activity.CreateClassActivity;
import com.ohuang.jiaxiaotong.Activity.JoinClassActivity;
import com.ohuang.jiaxiaotong.Activity.MainClassActivity;
import com.ohuang.jiaxiaotong.MyHttpURL;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.Sys;
import com.ohuang.jiaxiaotong.View.NoScrollListView;
import com.ohuang.jiaxiaotong.entity.ClassEntity;
import com.ohuang.jiaxiaotong.entity.EventMsg;
import com.ohuang.jiaxiaotong.entity.User;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;

//老师 班级
public class ClassFragment extends Fragment {
    private View view;
    private NoScrollListView noScrollListView;
    private SuperTextView button_create, button_join;
    private CommonAdapter commonAdapter;
    private List<ClassEntity> mData;
    private User user;
    private LoadDataLayout loadDataLayout;

    public ClassFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);//注册EventBus
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_class, container, false);
        initView();
        if (user != null) {
            loadDataLayout.showLoading("正在努力加载中", new LoadDataLayout.SetImgCallBack() {
                @Override
                public void setImg(ImageView img) {
                    Glide.with(getActivity())
                            .load(R.drawable.gif_loading)
                            .into(img);
                }
            });//显示 load布局 状态正在加载
            httpGetClass();//获取我的班级
        }
        return view;
    }

    private void initView() {
        noScrollListView = view.findViewById(R.id.listView_Class);
        button_create = view.findViewById(R.id.superTxt_createClass_class);
        button_join = view.findViewById(R.id.superTxt_joinClass_class);
        loadDataLayout = view.findViewById(R.id.loadData_class);
        //加入班级按钮
        button_join.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClickListener(SuperTextView superTextView) {
                if (Sys.isClick()) {
                    startActivity(new Intent(getContext(), JoinClassActivity.class));
                }
            }
        });

        //创建班级按钮
        button_create.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClickListener(SuperTextView superTextView) {
                if (Sys.isClick()) {
                    startActivity(new Intent(getContext(), CreateClassActivity.class));
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void GetUserEventBus(EventMsg eventMsg) {
        if (eventMsg.getCode() == 1) {
            httpGetClass();//数据更新了  重新获取我的班级信息
        }
    }

    //获取我加入的班级
    private void httpGetClass() {

        HashMap<String, String> map = new HashMap<>();
        map.put("username", user.getUsername());

        Ihttp.getInstance().post(MyHttpURL.mainUrl + "user/findClassByUser", map, ClassEntity.class, new CallBackObjects<ClassEntity>() {
            @Override
            public void success(List<ClassEntity> ojb) {
                mData = ojb;
                if (mData != null) {
                    if (mData.size() == 0) {
                        loadDataLayout.showEmpty();
                    } else {
                        loadDataLayout.showSuccess();
                    }
                    commonAdapter = new CommonAdapter(getContext(), R.layout.list_class, mData) {
                        @Override
                        protected void convert(ViewHolder viewHolder, Object item, int position) {
                            viewHolder.setText(R.id.txt_listClass, "班级名：" + ((ClassEntity) item).getClassName());
                        }
                    };

                    noScrollListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            if (Sys.isClick()) {
                                Intent intent = new Intent(getContext(), MainClassActivity.class);
                                intent.putExtra("className", mData.get(position).getClassName());
                                startActivity(intent);
                            }
                        }
                    });
                    noScrollListView.setAdapter(commonAdapter);
                }else {
                    loadDataLayout.showEmpty();
                }
            }


            @Override
            public void fail(String s) {
                loadDataLayout.showError();
            }
        });


    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //eventBus获取User
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void GetUserEventBus(User user) {
        this.user = user;
    }

}
